import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test01/config/config.dart';
import 'package:test01/model/user_model.dart';
 class ChangeInfoScreen extends StatefulWidget {
   const ChangeInfoScreen({Key? key}) : super(key: key);

   @override
   _ChangeInfoScreenState createState() => _ChangeInfoScreenState();
 }

 class _ChangeInfoScreenState extends State<ChangeInfoScreen> {
   UserModelItem _user = new UserModelItem();
   Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
   bool _allowUpdate = false;
   String _orgPassword = "";
   String _newPassword = "";

   _getUserInfo(int userId) async {
     var api = "${Config.domain}user/getUserById?userId=$userId";
     var result = await Dio().get(api);
     var userModel = UserModel.fromJson(result.data!);
     if (userModel.status == 200) {
       setState(() {
         _user = userModel.data!;
       });
     }
   }
   _changeAllowUpdate() {
     if (_orgPassword.length>=6 && _orgPassword != null && _newPassword.length>=6 && _newPassword!= null) {
       setState(() {
         _allowUpdate = true;
       });
     }
   }

   _updatePassword() async{
     var api = "${Config.domain}user/updatePassword?userId=${_user.userId}&&orgPassword=$_orgPassword&&newPassword=$_newPassword";
     var result = await Dio().post(api);
     return result.data;
   }

   @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _prefs.then((value) {
      if (value.getInt("userId") != null) {
        _getUserInfo(value.getInt("userId")!);
      }
    });
  }
   @override
   Widget build(BuildContext context) {
     return Scaffold(
       appBar: AppBar(
         leading: IconButton(
           onPressed: () {Navigator.pop(context);},
         icon: Icon(Icons.arrow_back_ios,color: Colors.black87,size: 40.w,)
     ),
         elevation: 0,
         centerTitle: true,
         backgroundColor: Colors.white,
         title: Text("修改密码",style: TextStyle(fontSize: 30.w, color: Colors.black87)),
       ),
       body: bulidMain(),
     );
   }

   Widget bulidMain() {
     if (_user.userId != null) {
       return Column(
           children:  [
             Container(
               padding: EdgeInsets.only(top: 20.w),
               child: const Text("设置新密码后通过用户名 + 密码进行登录", style: TextStyle(color: Colors.grey),),
             ),
             Container(
               height: 100.w,
               margin: EdgeInsets.only(top: 52.h),
               decoration: BoxDecoration(
                   color: Colors.white,
                   border: Border(bottom: BorderSide(width: 2.h, color: Colors.black26))
               ),
               child: Row(children: [
                 Container(
                   margin: EdgeInsets.only(left: 20.w),
                   color: Colors.white,
                   height: 56.h,
                   width: 100.w,
                   child: Text("用户名", style: TextStyle(fontSize: 30.w, color: Colors.black87)),
                 ),
                 Expanded(
                   child: Container(
                     padding: EdgeInsets.only(left: 30.w),
                     child: Text(_user.nickName!),
                   ),
                 )
               ]),
             ),
             Container(
               decoration: BoxDecoration(
                   color: Colors.white,
                   border: Border(bottom: BorderSide(width: 2.h, color: Colors.black26))
               ),
               child: Row(children: [
                 Container(
                   margin: EdgeInsets.only(left: 20.w),
                   color: Colors.white,
                   height: 56.h,
                   width: 100.w,
                   child: Text("原密码", style: TextStyle(fontSize: 30.w, color: Colors.black87)),
                 ),
                 Expanded(
                   child: Container(
                     padding: EdgeInsets.only(left: 30.w),
                     child: TextField(
                       onChanged: (value) {
                         _orgPassword = value;
                         if (value.length >=  6) {
                           _changeAllowUpdate();
                         } if(value.length < 6) {
                           setState(() {
                             _allowUpdate = false;
                           });
                         }
                       },
                       cursorColor: Colors.black,
                       cursorHeight: 56.h,
                       decoration: InputDecoration(
                           hintText: "请输入原密码",
                           hintStyle: TextStyle(fontSize: 30.w, color: Colors.black26),
                           border: InputBorder.none),
                     ),
                   ),
                 )
               ]),
             ),
             Container(
               decoration: BoxDecoration(
                   color: Colors.white,
                   border: Border(bottom: BorderSide(width: 2.h, color: Colors.black26))
               ),
               child: Row(children: [
                 Container(
                   margin: EdgeInsets.only(left: 20.w),
                   color: Colors.white,
                   height: 56.h,
                   width: 100.w,
                   child: Text("新密码", style: TextStyle(fontSize: 30.w, color: Colors.black87)),
                 ),
                 Expanded(
                   child: Container(
                     padding: EdgeInsets.only(left: 30.w),
                     child: TextField(
                       onChanged: (value) {
                         _newPassword = value;
                         if (value.length >=  6) {
                           setState(() {
                             _changeAllowUpdate();
                           });
                         } if(value.length < 6) {
                           setState(() {
                             _allowUpdate = false;
                           });
                         }
                       },
                       obscureText: true,
                       cursorColor: Colors.black,
                       cursorHeight: 56.h,
                       decoration: InputDecoration(
                           hintText: "请输入新密码",
                           hintStyle: TextStyle(fontSize: 30.w, color: Colors.black26),
                           border: InputBorder.none),
                     ),
                   ),
                 )
               ]),
             ),
             Container(
               padding: EdgeInsets.only(top: 35.h),
               child: ElevatedButton(
                 onPressed: _allowUpdate == false ? null : () {
                   Future<dynamic> req = _updatePassword();
                   req.then((value) {
                     if (value["status"] == 200) {
                       Navigator.pushNamed(context, "/");
                     }
                     else {
                       showDialog(
                         context: context,
                         builder: (context) {
                           return AlertDialog(
                             title: Container(
                                 alignment: Alignment.center,
                                 child: Text(value["msg"],
                                   style: TextStyle(fontSize: 30.w),)
                             ),
                             actions: [
                               Container(
                                 alignment: Alignment.center,
                                 child: OutlinedButton(onPressed: () {
                                   Navigator.pop(context);
                                 }, child: const Text("确定", style: TextStyle(
                                     color: Colors.lightGreen
                                 ),)),
                               )
                             ],
                           );
                         },
                       );
                     }
                   });
                 },
                 child: Container(
                   alignment: Alignment.center,
                   child: const Text("提交"),
                   width: 200.w,
                 ),
                 style: ElevatedButton.styleFrom(primary: Colors.lightGreen),
               ),
             ),
           ]
       );
     }
     else return const Text("");
   }
 }
